package middleware

import (
	"bp/internal/util"
	"context"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/status"
)

// UnaryLoggingInterceptor log grp request
func UnaryLoggingInterceptor(slog *zap.SugaredLogger) grpc.UnaryServerInterceptor {
	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
		clog := util.NewContextLogger(ctx, slog)
		clog.Info("enter-logger::")
		// log.Info(req)
		resp, err = handler(ctx, req)
		// log.Info(err)
		_, ok := status.FromError(err)
		if !ok {
			clog.Infof("exit-logger record error::%+v", err)
		} 
		// else {
		// 	clog.Infof("exit-logger response::%+v", resp)
		// }
		return resp, err
	}
}
